package org.ccay.manage.registry.servlet;

import java.io.IOException;

import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import org.ccay.core.dispatcher.EventUtil;
import org.ccay.core.exception.ApplicationException;
import org.ccay.core.registry.IRegistryManager;
import org.ccay.registry.events.IRegistryChangedHandler;
import org.ccay.registry.events.RegistryChangedEventArgs;


/**
 * 注册项同步服务
 */
@Named("/servlet/registry")
public class RegistrySyncServlet extends HttpServlet  { 
	private static final long serialVersionUID = 5037547880439900799L;
	/**
	 * 缓存管理器，可能为ehCache或者iCache
	 */
	@Inject
	private IRegistryManager registryManager ;
	/* (non-Javadoc)
	 * @see www.ccay.org.ccay.web.support.servlet.AbstractInternalServlet#doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		String registryItemName = request.getParameter("name");
		registryManager.reload(registryItemName);
		//发送事件通知
		try {
			EventUtil.dispatch(IRegistryChangedHandler.class, this, new RegistryChangedEventArgs(registryItemName), false);
		} catch (ApplicationException e) {
			e.printStackTrace();
		}
	}
}
